/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import model.LogFile;
import pojo.CMST;

/**
 *
 * @author A7med Kotb
 */
public class MainScheduleTaskDao {

    private Connection c;

    public MainScheduleTaskDao(Connection c) {
        this.c = c;
    }

    //method to add main schedule task data in db
    public boolean add(CMST cmst) {
        boolean check = false;
        try {
            PreparedStatement ps = c.prepareStatement("insert into createmainscheduletask(task_name,schedule,modifier,start_date,start_time,day,type,month)values(?,? , ? , ? , ? , ? , ? , ?)");
            ps.setString(1, cmst.getTaskName());
            ps.setString(2, cmst.getSchedule());
            ps.setString(3, cmst.getModifier());
            ps.setDate(4, cmst.getStartDate());
            ps.setString(5, cmst.getStartTime());
            ps.setString(6, cmst.getDay());
            ps.setString(7, cmst.getTaskType());
            ps.setString(8, cmst.getMonth());
            int i = ps.executeUpdate();
            if (i == 1) {
                check = true;
                LogFile.log(" System ", "Successfully inserted Data of Main Task Called ["+cmst.getTaskName()+"] into database");
            }
            else{
                LogFile.log("System ", " failed to insert data of Main Task Called ["+cmst.getTaskName()+"] into database");
                check = false;
            }
        } catch (SQLException ex) {
            check = false;
            LogFile.log(" System ", " encoutered the following exception while trying to add main schedule task data to Database ["+ex+"]");
            ex.printStackTrace();
        }
        return check;
    }

    //method to delete main task data from database
    public boolean delete(String taskName) {
        boolean check = false;
        try {
            PreparedStatement pstmt = c.prepareStatement("delete from createmainscheduletask where task_name=?");
            pstmt.setString(1, taskName);
            int i = pstmt.executeUpdate();
            if (i == 1) {
                check = true;
            }
        } catch (SQLException ex1) {
            check = false;
            LogFile.log(" System ", " encountered the following Exception while trying to delete main task data with name " + taskName + "and this is the error" + ex1.getMessage());
            ex1.printStackTrace();
        }
        return check;
    }

    //method to update main task data in database
    public boolean update(CMST cmst) {
        boolean check = false;
        try {
            boolean first = false;
            String updatetxt = "";
            if (cmst.getSchedule() != null) {
                first = true;
                updatetxt = "schedule='" + cmst.getSchedule() + "'";
            }
            if (cmst.getModifier() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "modifier='" + cmst.getModifier() + "'";
            }
            if (cmst.getStartDate() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "start_date='" + cmst.getStartDate() + "'";
            }
            if (cmst.getStartTime() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "start_time='" + cmst.getStartTime() + "'";
            }
            if (cmst.getDay() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "day='" + cmst.getDay() + "'";
            }
            if (cmst.getTaskType() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "type='" + cmst.getTaskType() + "'";
            }
            if (cmst.getMonth() != null) {
                if (first) {
                    updatetxt += ", ";
                }
                first = true;
                updatetxt += "month='" + cmst.getMonth() + "'";
            }
            if (first) {
                updatetxt += " , ";
            }
            updatetxt += " isenable = " + cmst.getIsEnable() + " , isrunning = " + cmst.getIsRunning() + " ";
            PreparedStatement ps = c.prepareStatement("update createmainscheduletask set " + updatetxt + " where task_name=?");
            System.out.println("update scheduledata set " + updatetxt + " where task_name=?");
            ps.setString(1, cmst.getTaskName());
            int i = ps.executeUpdate();
            if (i == 1) {
                check = true;
            }
        } catch (SQLException ex) {
            check = false;
            LogFile.log(" System ", " encountered the following error while trying to update data of main task with name : " + cmst.getTaskName() + " and this is the error " + ex.getMessage());
            ex.printStackTrace();
        }
        return check;
    }

    public Vector<CMST> getall() {
        Vector<CMST> tasks = new Vector<CMST>();
        try {
            PreparedStatement ps = c.prepareStatement("select * from createmainscheduletask");
            ResultSet result = ps.executeQuery();
            String str = "";
            while (result.next()) {
                CMST sc = new CMST();
                System.out.println("sccccccccccccccccccc" + result.getString(2));
                sc.setMstId(result.getInt(1));
                sc.setSchedule(result.getString(2));
                sc.setModifier(result.getString(3));
                if (result.getString(4) != null) {
                    sc.setStartDate(result.getDate(4));
                }
                if (result.getString(5) != null) {
                    sc.setStartTime(result.getString(5));
                }
                if (result.getString(6) != null) {
                    sc.setDay(result.getString(6));
                }
                sc.setTaskName(result.getString(7));
                sc.setTaskType(result.getString(8));
                sc.setIsEnable(result.getBoolean(9));
                sc.setIsRunning(result.getBoolean(10));
                if (result.getString(11) != null) {
                    sc.setMonth(result.getString(11));
                }
                tasks.add(sc);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return tasks;
    }

    //method to return book tasks
    public CMST getBookTask() {
        CMST sc = null;
        try {
            PreparedStatement ps = c.prepareStatement("select * from createmainscheduletask where type = 'book'");
            ResultSet result = ps.executeQuery();
            String str = "";
            if (result.next()) {
                sc = new CMST();
                System.out.println("sccccccccccccccccccc" + result.getString(2));
                sc.setMstId(result.getInt(1));
                sc.setSchedule(result.getString(2));
                sc.setModifier(result.getString(3));
                if (result.getString(4) != null) {
                    sc.setStartDate(result.getDate(4));
                }
                if (result.getString(5) != null) {
                    sc.setStartTime(result.getString(5));
                }
                if (result.getString(6) != null) {
                    sc.setDay(result.getString(6));
                }
                sc.setTaskName(result.getString(7));
                sc.setTaskType(result.getString(8));
                sc.setIsEnable(result.getBoolean(9));
                sc.setIsRunning(result.getBoolean(10));
                if (result.getString(11) != null) {
                    sc.setMonth(result.getString(11));
                }
            } else {
                sc = new CMST();
                sc.setTaskName("No Task Found");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return sc;
    }

    //method to return media tasks 
    public CMST getMediaTask() {
        CMST sc = null;
        try {
            PreparedStatement ps = c.prepareStatement("select * from createmainscheduletask where type = 'media'");
            ResultSet result = ps.executeQuery();
            String str = "";
            if (result.next()) {
                sc = new CMST();
                System.out.println("sccccccccccccccccccc" + result.getString(2));
                sc.setMstId(result.getInt(1));
                sc.setSchedule(result.getString(2));
                sc.setModifier(result.getString(3));
                if (result.getString(4) != null) {
                    sc.setStartDate(result.getDate(4));
                }
                if (result.getString(5) != null) {
                    sc.setStartTime(result.getString(5));
                }
                if (result.getString(6) != null) {
                    sc.setDay(result.getString(6));
                }
                sc.setTaskName(result.getString(7));
                sc.setTaskType(result.getString(8));
                sc.setIsEnable(result.getBoolean(9));
                sc.setIsRunning(result.getBoolean(10));
                if (result.getString(11) != null) {
                    sc.setMonth(result.getString(11));
                }
            } else {
                sc = new CMST();
                sc.setTaskName("No Task Found");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return sc;
    }

    //method to return tasks by name
    public CMST getask(String taskName) {
        try {
            PreparedStatement ps = c.prepareStatement("select * from createmainscheduletask where task_name=?");
            ps.setString(1, taskName);
            ResultSet result = ps.executeQuery();
            String str = "";
            result.next();
            CMST sc = new CMST();
            sc.setMstId(result.getInt(1));
            sc.setSchedule(result.getString(2));
            sc.setModifier(result.getString(3));
            sc.setStartDate(result.getDate(4));
            sc.setStartTime(result.getString(5));
            sc.setDay(result.getString(6));
            sc.setTaskName(result.getString(7));
            sc.setTaskType(result.getString(8));
            sc.setIsEnable(result.getBoolean(9));
            sc.setIsRunning(result.getBoolean(10));
            sc.setMonth(result.getString(11));
            return sc;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
